package com.ny.controller;
import com.ny.entity.Device;
import com.ny.entity.Result;
import com.ny.mapper.DeviceMapper;
import com.ny.service.DeviceService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.DeleteMapping;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import java.util.Date;
import java.util.List;

@RestController
public class DeviceController {
    //查询所有设备信息网页：http://127.0.0.1:8081/findAll
    @Autowired
    private DeviceService deviceService;

    @GetMapping("/findAll")
    public Result findAll() {
        return Result.success(deviceService.findAll());
    }

//根据设备名称查询设备信息网页：http://127.0.0.1:8081/search?name=设备名称
    @GetMapping("/search")
    public Result searchDevicesByName(@RequestParam("name") String deviceName) {
        try {
            List<Device> devices = deviceService.findByDeviceName(deviceName);
            return Result.success(devices);
        } catch (Exception e) {
            return Result.error("查询设备信息失败: " + e.getMessage());
        }
    }

    
    // 根据设备ID删除设备网页：http://127.0.0.1:8081/delete?id=设备ID
    @DeleteMapping("/delete")
    public Result deleteDevice(@RequestParam("id") Integer deviceId) {
        try {
            deviceService.deleteDeviceById(deviceId);
            return Result.success("删除成功");
        } catch (Exception e) {
            return Result.error("删除设备失败: " + e.getMessage());
        }
    }

    // 查询这个时间且状态是离线的设备信息网页：http://127.0.0.1:8081/findOfflineByTime?startTime=开始时间
    @GetMapping("/findOfflineByTime")
    public Result findOfflineDevicesByTime(
            @RequestParam("startTime") Date startTime) {
        try {
            List<Device> devices = deviceService.findOfflineDevicesByTime(startTime);
            return Result.success(devices);
        } catch (Exception e) {
            return Result.error("查询离线设备信息失败: " + e.getMessage());
        }
    }
}